//
//  ZYCPhotoCollectionCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/27.
//

#import "ZYCPhotoCollectionCell.h"
#import <UIImageView+WebCache.h>

@interface ZYCPhotoCollectionCell ()


@property (nonatomic, strong) UIButton *deleteButton;
@end
@implementation ZYCPhotoCollectionCell

- (void)setType:(ZYCTeaLeaveAddPhotoType)type{
    _type = type;
    if (type == ZYCTeaLeaveAddPhotoTypeShow) {
        _deleteButton.hidden = YES;
    }else{
        _deleteButton.hidden = NO;
    }
}
- (void)setObject:(NSObject *)object{
    _object = object;
    
    if ([object isKindOfClass:[ZYCBusFileListModel class]]) {
        ZYCBusFileListModel * model = (ZYCBusFileListModel *)object;
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],model.filePath]];
        [self.iconImageView sd_setImageWithURL:url];
    }else{
        UIImage * image = (UIImage *)object;
        self.iconImageView.image = image;
    }
}

- (void)setModel:(ZYCBusFileListModel *)model{
    _model = model;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/zd-system-service%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"sdfz_url"],model.filePath]];
    [self.iconImageView sd_setImageWithURL:url];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.iconImageView];
        [self addSubview:self.deleteButton];
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right);
            make.top.equalTo(self.mas_top);
            make.size.mas_equalTo(CGSizeMake(24, 24));
        }];
    }
    return self;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconImageView;
}


- (UIButton *)deleteButton {
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setImage:[UIImage imageNamed:@"sc"]  forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(clickDeleteAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

- (void)clickDeleteAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(photoCollectionCellDidClickDelete:)]) {
        [self.delegate photoCollectionCellDidClickDelete:self];
    }
}
@end
